from functools import partial

from PySide2 import QtCore, QtGui, QtWidgets
from . import resources_rc
from .widget_welcome import WidgetWelcome
from .widget_thickness import WidgetThickness
from .widget_calc import WidgetCalc
from .widget_angle import WidgetAngle


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('地质导向角差轨迹计算软件')

        # 创建工具栏
        self.tool_bar = QtWidgets.QToolBar()
        self.tool_bar.setMovable(False)
        self.addToolBar(QtCore.Qt.TopToolBarArea, self.tool_bar)

        # 创建堆栈小部件并添加页面
        self.stacked_widget = QtWidgets.QStackedWidget()
        widget_welcome = WidgetWelcome()
        self.stacked_widget.addWidget(widget_welcome)  # 索引 0（欢迎页）
        self.stacked_widget.addWidget(WidgetThickness())  # 索引 1（真厚度计算）
        self.stacked_widget.addWidget(WidgetCalc())  # 索引 2（角差计算）
        self.stacked_widget.addWidget(WidgetAngle())  # 索引 3（真倾角转换）
        self.setCentralWidget(self.stacked_widget)

        widget_welcome.doubleClicked.connect(partial(self.stacked_widget.setCurrentIndex, 1))

        # 添加工具栏按钮并绑定切换逻辑
        actions = [
            (QtGui.QIcon(':/images/welcome'), '欢迎页', 0),
            (QtGui.QIcon(':/images/thickness'), '真厚度计算', 1),
            (QtGui.QIcon(':/images/calc'), '角差计算和A点预测', 2),
            (QtGui.QIcon(':/images/angle'), '真倾角转换', 3),
        ]

        for icon, text, page_idx in actions:
            action = QtWidgets.QAction(icon, text, self)
            action.triggered.connect(partial(self.stacked_widget.setCurrentIndex, page_idx))
            self.tool_bar.addAction(action)
